package com.ticket.sass.admin.dto.output.operationlog;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.Gson;
import com.ticket.sass.admin.util.GsonUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.ticket.sass.admin.annotation.Module;
import java.time.LocalDateTime;

/**
 * @author ywb
 * @date 2024-03-08 16:36
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("操作日志数据")
public class OperationLogListOutput {
    @ApiModelProperty(value = "管理员 id")
    private Long adminId;
    
    @ApiModelProperty(value = "管理员 id")
    private String adminName;
    
    @ApiModelProperty(value = "操作模块，程序内部定义枚举")
    private String module;
    
    @ApiModelProperty(value = "操作模块名")
    private String moduleName;
    
    @ApiModelProperty(value = "操作描述")
    private String desc;
    
    @JsonIgnore
    @ApiModelProperty(value = "操作数据")
    private String data;
    
    @ApiModelProperty(value = "操作数据转换为Map")
    private Object dataObj;
    
    @ApiModelProperty(value = "创建时间")
    public LocalDateTime createdAt;
    
    public Object getDataObj() {
        if (data != null && !data.isEmpty()) {
            if ("{}".equals(data)) {
                return null;
            }
            try {
                Gson gson = GsonUtil.getInstance();
                this.dataObj = gson.fromJson(data, Object.class);
            } catch (Exception e) {
                e.printStackTrace(); // 这里应该根据实际情况处理异常
            }
        }
        return dataObj;
    }
    
    public String getModuleName() {
        return Module.valueOf(module).getChineseName();
    }
}
